<?php

namespace app\selladmin\controller;

use think\Controller;
use app\selladmin\model\Selladmin;
use think\Request;

class LoginController extends Controller {
    /*
     * 登录页面
     */

    public function index() {
        return view('index');
    }

    /*
     * 登陆
     */

    public function login() {
        $admin_name = input('post.admin_name');
        $admin_pwd = input('post.admin_pwd');

        $admin = new Selladmin();

        $row = $admin->where('admin_name', '=', $admin_name)
                ->where('admin_pwd', '=', md5($admin_pwd))
                ->where('status', '=', Selladmin::STATUS_1)
                ->find();

        if ($row) {
//            Session::set('name', 'thinkphp');
            session(config('adminconfig.USER_AUTH_KEY'), $row['id']);
            session("S_NAME", $row['admin_note']);
            session("S_ID", $row['id']);
            session("S_PHOTO", $row['photo']);
            session("S_ROLE", $row['role']);

            $data = [
                "last_time" => time(),
                "login_ip" => Request::instance()->ip(),
            ];

            $admin->where('id', '=', $row['id'])->update($data);

            if ($row['admin_name'] == config('adminconfig.RBAC_SUPERADMIN')) {
                session(config('adminconfig.ADMIN_AUTH_KEY'), true);
            }

            /* $Rbac = new \Org\Util\Rbac;
              $Rbac->saveAccessList(); */
            $this->success("登录成功", url('selladmin/index/index', '', false));
        } else {
            $this->error('登录失败');
        }
    }

}
